#include "Console.h"

void Console::addFunction(string name, CommandFunction func) {
	mFunction[name] = func;
}

void Console::deleteFunction(string name) {
	mFunction.erase(name);
}

void Console::enableFunction(string name) {
	auto f = mDisabled.find(name);
	if (f != mDisabled.end()) {
		mFunction[f->first] = f->second;
		mDisabled.erase(f);
	}
}

void Console::disableFunction(string name) {
	auto f = mFunction.find(name);
	if (f != mFunction.end()) {
		mDisabled[f->first] = f->second;
		mFunction.erase(f);
	}
}

void Console::enableFunction(vector<string>& names) {
	for (const string& name : names) {
		enableFunction(name);
	}
}

void Console::disableFunction(vector<string>& names) {
	for (const string& name : names) {
		disableFunction(name);
	}
}

void Console::queueCommand(string commandLine) {
	stringstream ss(commandLine);
	string functionName;
	ss >> functionName;
	auto iter = mFunction.find(functionName);

	if (iter != mFunction.end()) {

		string parameterString;
		vector<Parameter> parameters;
		while (ss) {
			ss >> parameterString;
			if (parameterString.length() > 0) {
				parameters.push_back(Parameter(parameterString));
			}
		}

		commandBuffer.push_back(Command(iter->second, std::move(parameters)));
	}

}

void Console::run() {
	for (Command& command : commandBuffer) {
		command.run();
	}
	commandBuffer.clear();
}
